<?php
/*
 * File:LinkDAO.php
 * Created:2013-05-17
 * Author:oldjiang
 * 
 */
class LinkDAO{
	private $table = 'link';
	private $query;
	
	function LinkDAO(){
		$this -> query = new Operate();
	}
	
	/**
	 * 添加一条新的link
	 */
	function add($linkPO){
		$linkArray = array();
		$linkArray['title'] = $linkPO -> getTitle();
		$linkArray['content'] = $linkPO -> getContent();
		$linkArray['url'] = $linkPO -> getUrl();
		$linkArray['`time`'] = $linkPO -> getTime();
		$linkArray['`mod`'] = $linkPO -> getMod();
		$sql = "insert into ".$this -> table." (title,content,url,`time`,`mod`) select '".$linkArray['title']."','".$linkArray['content']."','".$linkArray['url']."','".$linkArray['`time`']."','".$linkArray['`mod`']."' FROM DUAL where not exists(select * from ".$this -> table." where url='".$linkArray['url']."')";
		$this -> query -> query($sql);
	}
	
	/**
	 * 更新记录
	 */
	function update($linkPO){
		$sql = "update ".$this -> table." set title='".$linkPO -> getTitle()."',content='".$linkPO -> getContent()."',`time`='".$linkPO -> getTime()."',`mod`=".$linkPO -> getMod()." where url='".$linkPO -> getUrl()."'";
		$this -> query -> query($sql);
	}
	
	/**
	 * 判断某个URL在数据库中是否存在
	 */
	function getOneByUrl($url){
		$sql = "select * from ".$this -> table." where url='".$url."'";
		return $this -> query -> get_one($sql);
	}
	
	/**
	 * 搜索数据
	 */
	function search($sql){
		$sql = "select * from ".$this -> table." where ".$sql;
		return $this -> query -> get_all($sql);
	}
	
	/**
	 * 根据SQL语句获取记录总数
	 */
	function total($sql){
		$sql = "select count(*) from ".$this -> table." where ".$sql;
		$total =$this -> query -> get_one($sql);
		return $total['count(*)'];
	}
	
}
?>